package com.jsmile.cloud.authc.sys.repo;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.authc.api.req.sys.ReqSysResourceQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysRolePageQuery;
import com.jsmile.cloud.authc.api.res.ResSysRoleResources;
import com.jsmile.cloud.authc.sys.dao.SysRoleDao;
import com.jsmile.cloud.authc.sys.model.SysRole;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.SfEnum;
import com.jsmile.mall.api.kit.DateKit;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年10月16日 02:46:20
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SysRoleSearchRepository extends ServiceImpl<SysRoleDao, SysRole> {

    @Autowired
    private SysResourcesSearchRepository sysResourcesSearchRepository;

    // 获取用户id对应的权限
    public ResSysRoleResources getRoleSources(Long userId) {
        ResSysRoleResources resSysRoleResources = new ResSysRoleResources();
        List<SysRole> roles = getRoleByUserId(userId);
        roles.stream().forEach(x -> {
            if (JSmileConstant.JSMILE.SUPER_ADMIN_ROLE_ID == x.getId()) {// 超级管理员
                ReqSysResourceQuery reqSysResourceQuery = new ReqSysResourceQuery();
                reqSysResourceQuery.setCurTenantId(x.getTenantId());
                reqSysResourceQuery.setEnable(SfEnum.S.getValue());
                resSysRoleResources.setPermissions(sysResourcesSearchRepository.listToRes(reqSysResourceQuery));
            }
        });
        if (CollectionUtils.isEmpty(resSysRoleResources.getPermissions())) {
            resSysRoleResources.setPermissions(sysResourcesSearchRepository.getMenuByRoleIds(null, roles, SfEnum.F.getValue()));
        }
        return resSysRoleResources;
    }

    public List<SysRole> getRoleByUserId(Long userId) {
        return this.baseMapper.getRoleByUserId(userId);
    }

    public IPage<SysRole> pageQuery(ReqSysRolePageQuery reqSysRolePageQuery) {
        return this.baseMapper.selectPage(reqSysRolePageQuery.getPage(), getQueryWrapper(reqSysRolePageQuery));
    }

    private Wrapper getQueryWrapper(ReqSysRolePageQuery reqSysRoleQuery) {
        return new QueryWrapper<SysRole>().lambda().eq(null != reqSysRoleQuery.getCurTenantId(), SysRole::getTenantId, reqSysRoleQuery.getCurTenantId())
            .like(Objects.nonNull(reqSysRoleQuery.getRoleName()), SysRole::getRoleName, reqSysRoleQuery.getRoleName())
            .eq(null != reqSysRoleQuery.getRoleCode(), SysRole::getRoleCode, reqSysRoleQuery.getRoleCode())
            .eq(null != reqSysRoleQuery.getSysNo(), SysRole::getSysNo, reqSysRoleQuery.getSysNo())
            .eq(null != reqSysRoleQuery.getEnable(), SysRole::getEnable, reqSysRoleQuery.getEnable())
            .ge(null != reqSysRoleQuery.getStartDate(), SysRole::getCreateTime, DateKit.beginOfDay(reqSysRoleQuery.getStartDate()))
            .le(null != reqSysRoleQuery.getEndDate(), SysRole::getCreateTime, DateKit.endOfDay(reqSysRoleQuery.getEndDate())).orderByDesc(SysRole::getCreateTime);// sort,createTime倒序
    }
}
